﻿using JewererBasic.BO;
using JewererBasic.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace JewererBasic.Controllers
{
    public class ComponentOrderController : Controller
    {
        private MSQDBEntities db = new MSQDBEntities();

        //
        // GET: /ComponentOrder/

        public ActionResult Index()
        {
            var model = Component_Order_BO.GetTodayComponentOrder();
            return View(model);
        }

        public ActionResult IndexSubmit(List<COMPONENT_ORDER_DETAIL> models, int id = -1)
        {
            // Save to database
            bool result = Component_Order_BO.AddComponentOrderDetail(models);

            return Json(new { success = result }, JsonRequestBehavior.AllowGet);
        }

        static string component_id = "";

        public JsonResult ComponentAutoComplete(string query = "")
        {
            MSQDBEntities db = new MSQDBEntities();
            if (string.IsNullOrEmpty(query))
            {
                return Json(db.COMPONENTs.ToList(), JsonRequestBehavior.AllowGet);
            }
            var result = from d in db.COMPONENTs
                         where d.Id.Contains(query)
                         select d.Id;
            return Json(result.ToList(), JsonRequestBehavior.AllowGet);
        }

        public ActionResult GetComponentId(string query = "")
        {
            component_id = query;
            return Json(new {Success=true});
        }

        //public ActionResult GetListMolecule()
        //{
        //    var molecules = Component_Order_BO.GetMolecules(component_id);
        //    if (molecules != null)
        //    {

        //        List<COMPONENT_MOLECULE> result = new List<COMPONENT_MOLECULE>();
        //        foreach (var item in molecules)
        //        {
        //            COMPONENT_MOLECULE cm = new COMPONENT_MOLECULE();
        //            cm.Percentage = item.Percentage;
        //            cm.SlaveComponentId = item.SlaveComponentId;
        //            result.Add(cm);
        //        }
        //        return Json(new { Success = "True", molecules = result });
        //    }
        //    return Json(new { Success = "False", Message = "Can't find molecules is not correct!" });
        //}

        [HttpPost]
        public ActionResult Index(string Id = "")
        {
            return RedirectToAction("Index", "ComponentOrder");
        }
        
        public ActionResult Details(int id = -1)
        {
            ViewBag.Workers = this.db.EMPLOYEEs.ToList();

            COMPONENT_ORDER_DETAIL component_order_detail = db.COMPONENT_ORDER_DETAIL.Find(id);
            if (component_order_detail == null)
            {
                return HttpNotFound();
            }
            return View(component_order_detail);
        }
        
        [HttpPost]
        public ActionResult Details(COMPONENT_ORDER_DETAIL model, FormCollection form)
        {
            ViewBag.Workers = this.db.EMPLOYEEs.ToList();

            if (ModelState.IsValid)
            {
                if (form["submit"].Equals("Save"))
                {
                    Component_Order_BO.Update(model);
                    return RedirectToAction("Index");
                }
                else if (form["submit"].Equals("Finish"))
                {
                    Component_Order_BO.Finish(model);
                    return RedirectToAction("Index");
                }
                else //if(form["submit"].Equals("Export"))
                {
                    Component_Order_BO.Export(model.Id);
                    return RedirectToAction("Index");
                }
            }
            return View(model);
        }

        public ActionResult Delete(int id = 0)
        {
            bool result = Component_Order_BO.DeleteComponentOrderDetail(id);
            return RedirectToAction("Index");
        }

        public JsonResult GetWorkerId()
        {
            List<int> ids = this.db.EMPLOYEEs.Select(m => m.Id).ToList();
            List<string> staffIds = this.db.EMPLOYEEs.Select(m => m.Staff_Id).ToList();

            string values = "";
            foreach (var item in ids)
            {
                values += item.ToString() + ";";
            }
            string texts = "";
            foreach (var item in staffIds)
            {
                texts += item + ";";
            }

            return Json(new { values, texts }, JsonRequestBehavior.AllowGet);
        }
    }
}